<?php

class Admins_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = 'set_admins';
        $this->load->model("functions_model");
    }

    function newAdmin($added_by) {
        $data = $_POST;
        $data['added_by'] = $added_by;
        $data['last_modified'] = date("Y-m-d H:i:s");
        unset($data['password2']);
        $this->db->insert($this->tableName, $data);
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function updateAdmin($username) {
        $whr['username'] = $username;
        $data = $_POST;
        $data['last_modified'] = date("Y-m-d H:i:s");
        $this->db->update($this->tableName, $data, $whr);
    }

    function validateLogin() {
        $data = $_POST;
        $this->db->where($data);
        $this->db->from($this->tableName);
        if ($this->db->count_all_results() > 0) {
            $whr['username'] = $data['username'];
            $data = $_POST;
            $data['date_last_login'] = date("Y-m-d H:i:s");
            $this->db->update($this->tableName, $data, $whr);
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function getProfile($username) {
        $data['username'] = $username;
        $this->db->where($data);
        $this->db->from($this->tableName);
        $query = $this->db->get();
        return $query->row();
    }

    function getAll() {
        $this->db->select('tid,added_by, username,names,position,last_modified,date_last_login');
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            unset($row['tid']);
            $added_by = $row['added_by'];
            $row['added_by'] = "<a href='javascript:loadUrl(\"#admin_result\",\"" . base_url() . "index.php?/admin_mis/applicantProfile/$added_by\")'>" . $row['added_by'] . "</a>";
            $row['last_modified'] = $this->functions_model->prettyDateTime($row['last_modified']);
            $row['date_last_login'] = $this->functions_model->prettyDateTime($row['date_last_login']);
            $row['links'] = "<a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_settings/deleteAdmin/$tid\",\"Are you sure to delete this admin?\")'><img src='" . base_url() . "/images/delete.png' alt='Delete'/></a>";
            $result[] = $row;
        }
        return $result;
    }

}

?>
